var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

// 连接数据库
var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, result) {
    if (typeof result === 'undefined') {
      res.json({
        code: '1',
        msg: '检索失败'
      })
    } else {
      res.json(result)
    }
  }

// 检索用户接口
router.post('/getUserInfo', (req,res) => {
    var sql = $sql.user_info.searchAll
    var params = req.body
    console.log(params)
    conn.query(sql, [], function (err, result) {
      if (err) {
        console.log(err)
        return;
      }
      if (result) {
        jsonWrite(res, result)
      }
    })
})

// 增加用户接口
router.post('/insertUser', (req,res) => {
    var sql = $sql.user_info.insertUser
    var params = req.body
    console.log(params)
    conn.query(sql, [params.name,params.sex,params.registerDate,params.username,params.password], function (err, result) {
      if (err) {
        console.log(err)
        return;
      }
      if (result) {
        jsonWrite(res, result)
      }
    })
})

module.exports = router